﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using ZG_member.Model;
using ZG_member.BLL;

namespace ZG_member
{
    public partial class appointment : Form
    {
        public appointment()
        {
            InitializeComponent();
            skinEngine1.SkinFile = Application.StartupPath + ConfigurationManager.AppSettings["Skin"];
        }
        private void appointment_Load(object sender, EventArgs e)
        {
            cmb_ways.SelectedIndex = 0;
            cmb_Status.SelectedIndex = 0;
            getList("");
            this.timer1.Start();
        } 
        private void label5_Click(object sender, EventArgs e)
        {

        }

        private void btn_Add_Click(object sender, EventArgs e)
        {
            ZG_member.Model.informationShare Model = new ZG_member.Model.informationShare();
            ZG_member.BLL.informationShare  appoint_Bll=new BLL.informationShare();
            string time1=dtp_check_day.Text+" "+dtp_check_time.Text;
            string time2=dtp_appoint_day.Text+" "+dtp_appoint_time.Text;
            DateTime t = Convert.ToDateTime(time1);
            if (string.IsNullOrEmpty(txt_Name.Text))
            {
                MessageBox.Show("姓名不能为空");
                return;
            }
            if (string.IsNullOrEmpty(txt_Mobile.Text))
            {
                MessageBox.Show("联系方式不能为空");
                return;
            }
            Model.name = txt_Name.Text.Trim();
            Model.contact = txt_Mobile.Text.Trim();
            Model.appointmentWays = cmb_ways.SelectedItem.ToString();
            Model.status = cmb_Status.SelectedItem.ToString();
            Model.createDate = Convert.ToDateTime(time1);
            Model.appointmentDate = Convert.ToDateTime(time2);
            Model.memo = txt_Memo.Text;
            if (appoint_Bll.Add(Model) > 0)
            {
                MessageBox.Show("添加成功");
                getList("");
            }
            else
            {
                MessageBox.Show("添加失败");
            }
        } 
        private void btn_Search_Click(object sender, EventArgs e)
        {
            string name = search_Name.Text.Trim();
            DateTime start_Time=Convert.ToDateTime(dtp_start_day.Text+" "+dtp_start_time.Text);
            DateTime end_Time = Convert.ToDateTime(dtp_end_day.Text + " " + dtp_end_time.Text);
            if (end_Time >= start_Time)
            {
                if (!string.IsNullOrEmpty(name))
                {
                    getList("appointmentDate >='" + start_Time + "' and appointmentDate <='" + end_Time + "' and name like '%" + name + "%' order by id desc");
                }
                else
                {
                    getList("appointmentDate >='" + start_Time + "' and appointmentDate <='" + end_Time + "' order by id desc");
                }
            }
            else
            {
                if (!string.IsNullOrEmpty(name))
                {
                    getList("appointmentDate >='" + start_Time + "' and name like '%" + name + "%' order by id desc");
                }
                else
                {
                    getList("appointmentDate >='" + start_Time + "' order by id desc");
                }
            }
        } 
        private void btn_Del_Click(object sender, EventArgs e)
        {
            if (dataGridView1.Rows.Count < 1)
            {
                return;
            }
            ZG_member.BLL.informationShare  appoint_Bll=new BLL.informationShare();
            int id = Convert.ToInt32(dataGridView1.CurrentRow.Cells["id"].Value);
            DialogResult dr = MessageBox.Show("确定删除？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
            if (dr == DialogResult.OK)
            {
                if (appoint_Bll.del(id) > 0)
                {
                    MessageBox.Show("删除成功");
                    getList("");
                }
                else
                {
                    MessageBox.Show("删除失败");
                }
            }
        }
        private void dataGridView1_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            UpdateAppointment Update = new UpdateAppointment(this);
            if (dataGridView1.CurrentRow.Cells["id"].Value != null)
            {
                int id = Convert.ToInt32(dataGridView1.CurrentRow.Cells["id"].Value);
                Update.textBox1.Text = id.ToString();
            }
            if (dataGridView1.CurrentRow.Cells["name"].Value != null)
            {
                Update.txt_Name.Text = dataGridView1.CurrentRow.Cells["name"].Value.ToString();
            }
            if (dataGridView1.CurrentRow.Cells["contact"].Value != null)
            {
                Update.txt_Mobile.Text = dataGridView1.CurrentRow.Cells["contact"].Value.ToString();
            }
            if (dataGridView1.CurrentRow.Cells["createDate"].Value != null)
            {
                Update.dtp_check_day.Text = dataGridView1.CurrentRow.Cells["createDate"].Value.ToString();
                Update.dtp_check_time.Text = dataGridView1.CurrentRow.Cells["createDate"].Value.ToString();
            }
            if (dataGridView1.CurrentRow.Cells["appointmentDate"].Value != null)
            {
                 Update.dtp_appoint_day.Text=dataGridView1.CurrentRow.Cells["appointmentDate"].Value.ToString();
                 Update.dtp_appoint_time.Text = dataGridView1.CurrentRow.Cells["appointmentDate"].Value.ToString();
            }
            if (dataGridView1.CurrentRow.Cells["appointmentWays"].Value != null)
            {
                Update.cmb_ways.SelectedItem = dataGridView1.CurrentRow.Cells["appointmentWays"].Value.ToString();
            }
            if (dataGridView1.CurrentRow.Cells["status"].Value != null)
            {
                 Update.cmb_Status.SelectedItem=dataGridView1.CurrentRow.Cells["status"].Value.ToString();
            }
            if (dataGridView1.CurrentRow.Cells["memo"].Value != null)
            {
                Update.txt_Memo.Text=dataGridView1.CurrentRow.Cells["memo"].Value.ToString();
            }
            Update.ShowDialog();
        }
        private void btn_Import_Click(object sender, EventArgs e)
        {
            ExportExcel("", dataGridView1);
        }

        public void ExportExcel(string fileName, DataGridView myDGV)
        {
            string saveFileName = "";
            //bool fileSaved = false;
            SaveFileDialog saveDialog = new SaveFileDialog();
            saveDialog.DefaultExt = "xls";
            saveDialog.Filter = "Excel文件|*.xls";
            saveDialog.FileName = fileName;
            saveDialog.ShowDialog();
            saveFileName = saveDialog.FileName;
            if (saveFileName.IndexOf(":") < 0) return; //被点了取消 
            Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
            if (xlApp == null)
            {
                MessageBox.Show("无法创建Excel对象，可能您的机子未安装Office.Excel组件");
                return;
            }
            Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks;
            Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
            Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];//取得sheet1 
            int colIndex = 0;
            //写入标题
            for (int i = 0; i < myDGV.ColumnCount; i++)
            {
                if (myDGV.Columns[i].Visible)
                {
                    colIndex++;
                    worksheet.Cells[1, colIndex] = myDGV.Columns[i].HeaderText;
                    //  worksheet.Cells[1, i] = myDGV.Columns[i].HeaderText;
                }
            }
            //写入数值
            for (int r = 0; r < myDGV.Rows.Count; r++)
            {
                colIndex = 0;
                for (int i = 0; i < myDGV.ColumnCount; i++)
                {
                    if (myDGV.Columns[i].Visible)
                    {
                        colIndex++;
                        worksheet.Cells[r + 2, colIndex] = myDGV.Rows[r].Cells[i].Value;
                        if (colIndex == 3)
                        {
                            worksheet.Cells[r + 2, colIndex] = "'" + myDGV.Rows[r].Cells[i].Value;
                            continue;
                        }
                    }
                }
                System.Windows.Forms.Application.DoEvents();
            }
            worksheet.Columns.EntireColumn.AutoFit();//列宽自适应

            if (saveFileName != "")
            {
                try
                {
                    workbook.Saved = true;
                    workbook.SaveCopyAs(saveFileName);
                    //fileSaved = true;
                }
                catch (Exception ex)
                {
                    //fileSaved = false;
                    MessageBox.Show("导出文件时出错,文件可能正被打开！/n" + ex.Message);
                }

            }
            xlApp.Quit();
            GC.Collect();//强行销毁 
            // if (fileSaved && System.IO.File.Exists(saveFileName)) System.Diagnostics.Process.Start(saveFileName); //打开EXCEL

            MessageBox.Show(fileName + "导出成功", "系统提示", MessageBoxButtons.OK);
        }
        private void getList(string strWhere)
        {
             ZG_member.BLL.informationShare  appoint_Bll=new BLL.informationShare();
             dataGridView1.DataSource = appoint_Bll.getAppointList(strWhere).Tables[0];
        }

       private void timer1_Tick(object sender, EventArgs e)
        {
            this.Left += 2;
            this.Opacity += 0.1;
            if (this.Opacity == 1)
            {
                this.timer1.Stop();
            }
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            this.Top = this.Top + 10;
            this.Opacity = this.Opacity - 0.06;
            if (Opacity <= 0)
            {
                this.timer2.Stop();
                this.Close();
            }
        }

        private void btn_Close_Click(object sender, EventArgs e)
        {
            this.timer2.Start();
        }

        private void txt_Mobile_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(((e.KeyChar >= '0') && (e.KeyChar <= '9')) || e.KeyChar <= 31))
            {
                if (e.KeyChar == '.')
                {
                    if (((TextBox)sender).Text.Trim().IndexOf('.') > -1)
                        e.Handled = true;
                }
                else
                    e.Handled = true;
            }
            else
            {
                if (e.KeyChar <= 31)
                {
                    e.Handled = false;
                }
                else if (((TextBox)sender).Text.Trim().IndexOf('.') > -1)
                {
                    if (((TextBox)sender).Text.Trim().Substring(((TextBox)sender).Text.Trim().IndexOf('.') + 1).Length >= 4)
                        e.Handled = true;
                }
            }
        }

       

        
    }
}
